package com.zxtc.wh.dao;

import com.zxtc.wh.pojo.resource.Resource;
import com.zxtc.wh.pojo.resource.ResourcePojo;
import com.zxtc.wh.utils.MybatisUtils;
import com.zxtc.wh.utils.SqlUtil;
import org.apache.ibatis.session.SqlSession;

import java.rmi.StubNotFoundException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 */
public class ResourceDaoImpl implements IResourceDao {

    private SqlUtil sqlUtil;
    private PreparedStatement selectByUuidStatement;

    SqlSession sqlSession;

    public ResourceDaoImpl() {
        this(MybatisUtils.getInstance());
    }

    public ResourceDaoImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    /**
     * 从数据库的查询结果中反系列化为{@link Resource}
     */
    public static Resource createByResultSet(ResultSet set) throws SQLException {
        if (set != null && set.next()) {
            Resource resource = new Resource();

            resource.setResource_id(set.getLong("resource_id"));
            resource.setRecource_uuid(set.getString("recource_uuid"));
//            resource.setRecource_uuid(UUID.fromString(set.getString("recource_uuid")));
//            resource.setResource_path(set.getString("resource_path"));
//            resource.setResource_file_name(set.getString("resource_file_name"));
//            resource.setResource_create_timestamp(set.getTimestamp("resource_create_timestamp"));
//            resource.setResource_size(set.getLong("resource_size"));
//            resource.setResource_hash(set.getString("resource_hash"));
            //TODO 当前只有默认的文件类型和默认的资源所有者
//            resource.setResource_type_uuid(set.getLong("resource_type_uuid"));
//            resource.setUser_uuid(set.getLong("user_uuid"));

            return resource;
        }

        return null;
    }


    @Override
    public ResourcePojo create() {
        return null;
    }

    @Override
    public ResourcePojo create(ResourcePojo data) {
//        throw new StubException();


        return data;
    }

    @Override
    public ResourcePojo insert(ResourcePojo resourcePojo) {
//        String sqlCmd;
//        try {
//            PreparedStatement statement;
//            UserPojo userPojo = resourcePojo.getUserPojo();
////            if (userPojo == null) {
////                userPojo = this.userDao.create();
////            }
//
//            sqlCmd = "SELECT user_uuid FROM USER where user_uuid = ? and user_uuid = ?";
//            statement = sqlUtil.getStatement(sqlCmd);
//            statement.setLong(1, userPojo.getUser_id());
//            statement.setString(2, userPojo.getUser_uuid());
//
//
//            sqlCmd = "INSERT INTO RESOURCE " +
//                    "( recource_uuid, resource_path, resource_file_name, resource_create_timestamp, resource_size, resource_hash, resource_type_uuid, user_uuid) " +
//                    "VALUES( ?, ?, ?, ?, ?, ?, ?, ?, ?)";
//            statement.setString(1, resourcePojo.getRecource_uuid());
////            statement.setString(2, muser.getResource_path());
////            statement.setString(3, muser.getResource_file_name());
////            statement.setTimestamp(4, new Timestamp(muser.getResource_create_timestamp().getTime()));
////            statement.setLong(5, muser.getResource_size());
////            statement.setString(6, muser.getResource_hash());
//            statement.setLong(7, resourcePojo.getResource_type().resource_type_id);
//            statement.setLong(7, userPojo.getUser_id());
//
//            return statement.executeUpdate();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }

        return null;
    }

    @Override
    public ResourcePojo updateOrCreate(ResourcePojo data) {
        return null;
    }

    @Override
    public Boolean isExists(Long id) {


        return null;
    }

//    @Override
//    public ResourcePojo getInstance(String uuid) {
//        try {
//            String sql = "select * from resource where resource_uuid=?";
//            this.selectByUuidStatement = sqlUtil.getStatement(sql);
//            selectByUuidStatement.setString(1, uuid);
//
//            ResultSet set = selectByUuidStatement.executeQuery();
//            Resource resource = createByResultSet(set);
//
//            return resource;
//        } catch (Exception e) {
//            return null;
//        }
//    }

    @Override
    public ResourcePojo getInstance(Long id) {
//        Object pojo = sqlSession.selectOne("getInstance", id);
//        Object pojo = sqlSession.selectOne("com.zxtc.wh.ResourceMapper.getInstance(ID)", id);
        Object pojo = sqlSession.selectCursor("com.zxtc.wh.dao.IResourceDao.getInstance", id);

//        IResourceDao mapper = sqlSession.getMapper(IResourceDao.class);
//        ResourcePojo pojo = mapper.getInstance(id);
        sqlSession.commit();

        System.out.println(pojo);
        return (ResourcePojo) pojo;
//        try {
//            if (selectByUuidStatement == null) {
//                String sql = "select * from resource where resource_id=?";
//                this.selectByUuidStatement = sqlUtil.getStatement(sql);
//                selectByUuidStatement.setLong(1, id);
//
//            }
//
//            assert selectByUuidStatement != null;
//            ResultSet set = selectByUuidStatement.executeQuery();
//            Resource resource = createByResultSet(set);
//
//            return resource;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
    }

    @Override
    public ResourcePojo update(ResourcePojo data) {
        return null;
    }

    @Override
    public void onError(Exception eMessage) {

    }

    private class StubException extends RuntimeException {
    }
}
